package com.bida.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bida.context.BaseContext;
import com.bida.domain.dto.ShoppingCartDTO;
import com.bida.domain.po.Dish;
import com.bida.domain.po.Setmeal;
import com.bida.domain.po.ShoppingCart;
import com.bida.mapper.ShoppingCartMapper;
import com.bida.result.Result;
import com.bida.service.IDishService;
import com.bida.service.ISetmealService;
import com.bida.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 服务实现类
 * </p>
 *
 * @author ly
 * @since 2024-09-07
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {


    @Autowired
    private IDishService dishService;

    @Autowired
    private ISetmealService setmealService;


    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public Result addShoppingCartInfo(ShoppingCartDTO shoppingCartDTO) {
        // 1.获取当前登录用户id
        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        String dishFlavor = shoppingCartDTO.getDishFlavor();

        // 2.条件构造器
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        if (userId != null){
            queryWrapper.eq("user_id", userId);
        }
        if (dishId != null){
            queryWrapper.eq("dish_id", dishId);
        }
        if (setmealId != null){
            queryWrapper.eq("setmeal_id", setmealId);
        }
        if(dishFlavor != null){
            queryWrapper.eq("dish_flavor", dishFlavor);
        }
        List<ShoppingCart> list = this.list(queryWrapper);
        if (list != null && !list.isEmpty()){
            // 3.如果已经存在了，只需要将数量加一
            ShoppingCart cart = list.get(0);
            this.lambdaUpdate()
                    .set(ShoppingCart::getNumber, cart.getNumber() + 1)
                    .eq(ShoppingCart::getId, cart.getId())
                    .update();
        }else {
            // 4.如果不存在，插入一条购物车数据
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
            shoppingCart.setNumber(1);

            if (dishId != null){
                Dish dish = dishService.lambdaQuery()
                        .eq(Dish::getId, dishId)
                        .one();
                shoppingCart.setUserId(userId);
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
            }else{
                Setmeal setmeal = setmealService.lambdaQuery()
                        .eq(Setmeal::getId, setmealId)
                        .one();
                shoppingCart.setUserId(userId);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            this.save(shoppingCart);
        }
        return Result.success();
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> getShoppingCartList() {
        // 1.获取当前登录用户id
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = this.lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .list();
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        // 1.获取当前登录用户id
        Long userId = BaseContext.getCurrentId();
        // 2.删除
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        this.remove(queryWrapper);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 1.获取当前登录用户id
        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        String dishFlavor = shoppingCartDTO.getDishFlavor();
        // 2.条件构造器
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        if (dishId != null){
            queryWrapper.eq("dish_id",dishId);
        }
        if (setmealId != null){
            queryWrapper.eq("setmeal_id",setmealId);
        }
        if (dishFlavor != null){
            queryWrapper.eq("dish_flavor",dishFlavor);
        }
        this.remove(queryWrapper);
    }


}
